<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../../util/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>添加部门</title>
</head>
<body>
<div id="editAllDepartmentForm">
        <div class="form-Container">
            <form action="${pageContext.request.contextPath}/department/editAllDepartment.do"  id="dataForm" class="grayForm" method="post">
            <input type="hidden" name="curCompanyUUID" value="${param.companyUUID }" />
                <table>
                    <tr>
                        <td style="text-align: right;">
                            <input type="button" value="新增一栏"    id="createRow" />
                            <input type="button" value="移除选中栏" name="close" id="removeRow" style="background-color: #E56100"/>
                        </td>
                    </tr>
                </table>
                <table id="contentTable">
                    <tr>
                        <td ><input type="checkbox"></td>
                        <td ><label style="width:79px" for="companyUUID">所属公司:</label></td>
                        <td style="line-height:20px;">
                           <select name="companyUUID" id="companyUUID"  class="select-box" curV="" >
                               <option value="">请选择</option>
                           </select>
                        </td>
                        <td ><label style="width:79px" for="departmentName">部门名字:</label></td>
                        <td style="line-height:20px;"><input type="text" required name="departmentName" /></td>
                    </tr>
                    <tr>
                        <td ><input type="checkbox"></td>
                        <td ><label style="width:79px" for="companyUUID">所属公司:</label></td>
                        <td style="line-height:20px;">
                           <select name="companyUUID" id="companyUUID"  class="select-box" curV="" >
                               <option value="">请选择</option>
                           </select>
                        </td>
                        <td ><label style="width:79px" for="departmentName">部门名字:</label></td>
                        <td style="line-height:20px;"><input type="text" required name="departmentName" /></td>
                    </tr>
                    <tr>
                        <td ><input type="checkbox"></td>
                        <td ><label style="width:79px" for="companyUUID">所属公司:</label></td>
                        <td style="line-height:20px;">
                           <select name="companyUUID" id="companyUUID"  class="select-box" curV="" >
                               <option value="">请选择</option>
                           </select>
                        </td>
                        <td ><label style="width:79px" for="departmentName">部门名字:</label></td>
                        <td style="line-height:20px;"><input type="text" required name="departmentName" /></td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td style="text-align: center;">
                            <input type="button" value="提交数据" name="submitData"  />
                            <input type="button" value="关闭" name="close" />
                        </td>
                    </tr>
                </table>
                <div id="confirmMsg"></div>
            </form>
        </div>
</div>
        <script type="text/javascript">
        $(document).ready(function () {
        	//加载单位数据
        	loadCompanyList();
        	
        	//表格新增一行
        	$("#editAllDepartmentForm #createRow").click(function(){
        		var firstTrDom = $("#editAllDepartmentForm #contentTable tr:first");
        		var trLength = $("#editAllDepartmentForm #contentTable tr").length+1;
        		var rowHtml = firstTrDom.html();
        		rowHtml = "<tr>"+rowHtml+"</tr>";
        		$("#editAllDepartmentForm #contentTable").append(rowHtml);
        	})
        	
        	//表格移除
        	$("#editAllDepartmentForm #removeRow").click(function(){
        		var trLength = $("#editAllDepartmentForm #contentTable tr").length
        		var checkedLength = $("#editAllDepartmentForm #contentTable input:checked").length;
        		if(trLength>checkedLength){
        			$("#editAllDepartmentForm #contentTable :checkbox").each(function(i){
            			var checked = $(this).attr("checked");
            			if(checked=="checked"){
            				$(this).parent().parent().remove();
            			}
            		})
        		}else{
        			$.messager.alert('我的消息','不可全部移除,至少保留一行！','info');
        		}
        	})
        	
            //提交数据到服务器
            $("#editAllDepartmentForm input[name='submitData']").click(function () {
            	//提交表单进行验证
                var actionUrl = $("#editAllDepartmentForm #dataForm").attr("action");
            	var departmentList = [];
            	$("#editAllDepartmentForm select[name='companyUUID']").each(function(i){
            		var companyUUID = $(this).val();
            		if($(this).val()!=""){
            			var department = new Object();
                		department.companyUUID = $(this).val();
                		department.departmentName = $("#editAllDepartmentForm input[name='departmentName']:eq("+i+")").val();
                		department.departmentDesc = department.departmentName;
                		departmentList.push(department);
            		}
            	})
            	var formData = $.toJSON(departmentList);
            	$.post(actionUrl,{formData:formData},function(data){
            		if(data.code=="00"){
        				  $.messager.confirm('操作确认',data.msg+',请确认是否继续操作？', function(flag){
        					  if(flag){
        						  $("#editDataFormArea").window("close");
        						  tableObject.datagrid('reload');
        					  }
        				  });
        			    }else{
        				   $.messager.alert('操作提示',data.message,'error');
        			    }
      		    },"json");
            });
            
        	if(id>0){
        		//加载表单数据
            	loadFormData(id+"");
        		//$("#confirmMsg").html("<font color=red><b>注：修改用户信息时，密码输入框留空则代表不修改密码！</b></font>");
        	}
        });
        
        //加载单位数据
        function loadCompanyList(){
        	$.post(getBasePath()+"company/getJsonAllList.do",{},function(jsonData){
        		var optionList = "<option value=\"\">请选择</option>";
        		var selectArea = $("#editAllDepartmentForm select[name='companyUUID']");
        		var curUUID = $("#editAllDepartmentForm input[name='curCompanyUUID']").val();
        		$.each(jsonData.data,function(i,item){
        			if(item.uuid==curUUID){
        				optionList+= "<option value=\""+item.uuid+"\" selected>"+item.companyName+"</option>";
        			}else{
        				optionList+= "<option value=\""+item.uuid+"\">"+item.companyName+"</option>";
        			}
        		})
        		selectArea.html(optionList);
  		    },"json");
        }
    </script>
</body>
</html>